package com.bdzl.auth.service.impl;

import com.bdzl.auth.service.MeauService;
import com.bdzl.pub.common.Tree;
import com.bdzl.pub.entity.sys.MeauDO;
import com.bdzl.pub.util.param.BuildTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bdzl.auth.dao.RoleMeauDao;
import com.bdzl.pub.entity.sys.RoleMeauDO;
import com.bdzl.auth.service.RoleMeauService;



@Service
public class RoleMeauServiceImpl implements RoleMeauService {
	@Autowired
	private RoleMeauDao roleMeauDao;
	@Autowired
	private MeauService meauService;
	
	@Override
	public RoleMeauDO get(Integer id){
		return roleMeauDao.get(id);
	}

	/**
	 * @Author Zhn
	 * @Description //TODO 根据角色查询菜单列表树状图
	 * @Date 11:52 2021/5/11
	 * @Param [roleId] 角色Id
	 * @return java.util.List<com.bdzl.pub.entity.sys.MeauDO>
	 **/
	@Override
	public List<Tree<MeauDO>> getMeauDOtreeByRoleId(Integer roleId,String type){
		List<MeauDO> meauDOList = this.getByRoleId(roleId,type);
		List<Tree<MeauDO>> trees = new ArrayList<Tree<MeauDO>>();
		for (MeauDO sysMenuDO : meauDOList) {
			Tree<MeauDO> tree = new Tree<MeauDO>();
			tree.setId(sysMenuDO.getMenuId());
			tree.setParentId(sysMenuDO.getMenuPid());
			tree.setText(sysMenuDO.getMeauName());
			tree.setIcon(sysMenuDO.getMeauIcon());
			Map<String, Object> attributes = new HashMap<>(16);
			attributes.put("url", sysMenuDO.getMenuUrl());
			attributes.put("icon", sysMenuDO.getMeauIcon());
			tree.setAttributes(attributes);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		List<Tree<MeauDO>> list = BuildTree.buildList(trees, "-1");
		return list;
	}

	@Override
	public List<RoleMeauDO> list(Map<String, Object> map){
		return roleMeauDao.list(map);
	}

	@Override
	public List<MeauDO> getByRoleId(Integer roleId, String type) {
		Map<String,Object> map = new HashMap<>();
		map.put("roleId",roleId);
		map.put("type",type);
		return roleMeauDao.getByRoleId(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return roleMeauDao.count(map);
	}
	
	@Override
	public int save(RoleMeauDO roleMeau){
		return roleMeauDao.save(roleMeau);
	}
	
	@Override
	public int update(RoleMeauDO roleMeau){
		return roleMeauDao.update(roleMeau);
	}

	@Override
	public int remove(Integer id){
		return roleMeauDao.remove(id);
	}

	@Override
	public int removeByRoleId(Integer roleId){
		return roleMeauDao.removeByRoleId(roleId);
	}
	
	@Override
	public int batchRemove(Integer[] ids){
		return roleMeauDao.batchRemove(ids);
	}

	@Override
	public List<String> listMenuIdByRoleId(String roleId,String meauClassify) {
		Map<String,Object> map = new HashMap<>();
		map.put("roleId",roleId);
		map.put("meauClassify",meauClassify);
		return roleMeauDao.listMenuIdByRoleId(map);
	}

}
